<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #root {
        background-color: dimgray;
      }
      #root div {
        width: 100px;
        height: 100px;
        margin: 2px;
        background-color: white;
      }
    </style>
  </head>
  <body>
    <div id="root"></div>

    <script>
      function slow() {
        for (let index = 0; index < 10000; index++) {
            let div = document.createElement("div")
            div.style.width = `${index}px`
            div.style.height = `${index}px`
            div.style.background = `red`
          document
            .querySelector("#root")
            .appendChild(div);
          if (index !== 0 && index % 5 === 0) {
            document
              .querySelector("#root")
              .removeChild(document.querySelectorAll("#root div")[index - 3]);
          }
          if (index !== 0 && index % 3 === 0) {
            let div = document.createElement("div")
            div.style.width = `${index}px`
            div.style.height = `${index}px`
            div.style.background = `green`
            document
              .querySelector("#root")
              .appendChild(div);
          }

        }
      }

      function fast() {
        let dom = [];
        for (let index = 0; index < 10000; index++) {
          if (index !== 0 && index % 5 === 0) {
            dom.splice(index - 4, 1);
          }
          if (index !== 0 && index % 3 === 0) {
            dom.push(`<div style='background:red;width:${index}px;height:${index}px'></div>`);
          }
          dom.push(`<div style='background:green;width:${index}px;height:${index}px'></div>`);
        }
        document.querySelector("#root").innerHTML = dom;
      }

      console.time();
      // slow();
        fast();
      console.timeEnd();
    </script>
  </body>
</html>
